<?php


	class WechatApi
{	
			var $appid;

			var $appsecret;

	public function __construct($appid,$appsecret){

			$this->appid = $appid;

			$this->appsecret = $appsecret;

	}	
	//开发者接口，验证签名

    public function get_user_info(){

		if (!isset($_GET['code']))
	{
			//触发微信返回code码
			
			$pdo = new PDO("mysql:host=localhost;dbname=zhetu","root","db692a4f05"); 
	        $pdo->query("set names utf8");
			
			$sql="select * from system where id=1";
	        $sys=$pdo->query($sql);
	        $sys=$sys->fetch();
			
		    $url=$sys['url']."/Oauth1.php?pid=".$_GET['pid'];
			 
			// echo $url;exit;
			 
			//$url = urlencode('http://'.$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI'].'?id='.$_GET['id']);
           $oauthurl = "https://open.weixin.qq.com/connect/oauth2/authorize?appid=$this->appid&redirect_uri=$url&response_type=code&scope=snsapi_base&state=STATE#wechat_redirect";
   			//$oauthurl = "https://open.weixin.qq.com/connect/oauth2/authorize?appid=$this->appid&redirect_uri=$url&response_type=code&scope=snsapi_userinfo&state=STATE#wechat_redirect";
			Header("Location: $oauthurl"); 
		}else
	{
			//获取code码，以获取openid
	    	$code = $_GET['code'];
			$api = "https://api.weixin.qq.com/sns/oauth2/access_token?appid=$this->appid&secret=$this->appsecret&code=$code&grant_type=authorization_code";

			$data = json_decode(file_get_contents($api),true);

			$openid = $data['openid'];//获取到的openid
			
			//echo $openid;exit;
			
			return $openid;

	}

 }

}
	
	$pdo = new PDO("mysql:host=localhost;dbname=zhetu","root","db692a4f05"); 
	$pdo->query("set names utf8");
	
	$sql="select * from system where id=1";
	$sys=$pdo->query($sql);
	$sys=$sys->fetch();
	
	
	$sql="select * from wx where id=".$sys['fangan'];
	$info=$pdo->query($sql);
	$info=$info->fetch();
	
	//var_dump($info);
	//die;
	  $obj = new WechatApi($info['appid'],$info['appsecret']);//填你的appid,appsecret
	  $result = $obj->get_user_info();
	  
	// var_dump($result);exit();
	  
	//  $url=$sys['tiaozhuanlianjie']."/index.php/tiaozhuan/index.html?openid=".$result;
	
	
	//var_dump($_GET);
	
	if($result){
	
   	 $url=$sys['tiaozhuanlianjie']."/index.php/tiaozhuan/index.html?openid=".$result."&pid=".$_GET['pid'];
	 
	 //var_dump($url);exit();
	 
	   
     Header("Location:$url");exit();
    
	}
	
?>